package cn.edu.scau.cmi.lintianwei.hibernate.dao;

import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Transaction;
import cn.edu.scau.cmi.lintianwei.hibernate.domain.Student;
import cn.edu.scau.cmi.lintianwei.hibernate.domain.Teacher;

public class TeacherDAO extends BaseHibernateDAO {

	public void save(Teacher teacher, Set<Student> students) {
		try {
			Transaction transactionT = getSession().beginTransaction();
			getSession().save(teacher);
			// 鎻愪氦浜嬪姟
			transactionT.commit();

			for (Student student : students) {
				Teacher oldT = student.getTeacher();
				student.setTeacher(teacher);
				new StudentDAO().update(student, oldT);
			}
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(Teacher teacher) {
		try {
			// 瑕佸垹闄よ�佸笀,蹇呴』鍏堝皢鍏宠仈鐨勫鐢熶俊鎭敼鎺�,瀛︾敓鍗曟柟闈㈣В闄ゅ叧绯�
			Set<Student> students = teacher.getStudents();
			for (Student student : students) {
				student.setTeacher(null);
				new StudentDAO().update(student);
			}

			Transaction transactionT = getSession().beginTransaction();
			getSession().delete(teacher);
			transactionT.commit();

		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void update(Teacher teacher, Set<Student> oldStudents) {
		try {
			// 浠ュ墠瀛︾敓鍗曟柟闈㈡柇鎺夊叧绯�
			for (Student student : oldStudents) {
				student.setTeacher(null);
				new StudentDAO().update(student);
			}
			// 鏂扮殑瀛︾敓鍗曟柟闈㈠缓绔嬪叧绯�
			Set<Student> students = teacher.getStudents();
			for (Student student : students) {
				// 寰楀埌涔嬪墠鐨勮�佸笀
				Teacher oldT = (student == null ? null : student.getTeacher());
				student.setTeacher(teacher);
				new StudentDAO().update(student, oldT);
			}
			// 鑰佸笀寤虹珛鍏崇郴
			Transaction transactionT = getSession().beginTransaction();
			getSession().update(teacher);
			transactionT.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void update(Teacher teacher) {
		try {
			Transaction transactionT = getSession().beginTransaction();
			getSession().update(teacher);
			transactionT.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Teacher findById(Long id) {
		try {
			Teacher instance = (Teacher) getSession().get("cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Teacher", id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from Teacher as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findAll() {
		try {
			String queryString = "from Teacher";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
